#include "soundmanager.h"
#include <QDebug>
#include "gamemanager.h"
SoundManager::SoundManager()
{
    bgmPlayer = new QMediaPlayer();
    bgmPlayer->setVolume(GameManager::Instance().bgmVolume);
    soundPlayer = new QMediaPlayer();
    bgmPlayer->setVolume(GameManager::Instance().soundVolume);
    bgsePlayer = new QMediaPlayer();
    bgmPlayer->setVolume(GameManager::Instance().bgseVolume);
    bgm = new QMediaPlaylist();
    bgse = new QMediaPlaylist();
}
SoundManager &SoundManager::Instance()
{
    static SoundManager instance;
    return instance;
}

SoundManager::~SoundManager()
{
    delete bgmPlayer;
    delete soundPlayer;
    delete bgsePlayer;
    delete bgm;
    delete bgse;
}

void SoundManager::PlayBGM(QString path,bool loop)
{
    if(loop)
    {
        bgm->clear();
        bgmPlayer->setPlaylist(bgm);
        bgm->addMedia(QUrl::fromLocalFile(path));
        bgm->setPlaybackMode(QMediaPlaylist::Loop);
        bgmPlayer->play();
        return;
    }
    if(bgmPlayer->state() == QMediaPlayer::QMediaPlayer::State::PausedState)
    {
        bgmPlayer->play();
        return;
    }
    bgmPlayer->setMedia(QUrl::fromLocalFile(path));
    //bgmPlayer->setVolume(50);
    bgmPlayer->play();
}

void SoundManager::Play(QString path)
{
    if(soundPlayer->state() == QMediaPlayer::QMediaPlayer::State::PausedState)
    {
        soundPlayer->play();
        return;
    }
    soundPlayer->setMedia(QUrl::fromLocalFile(path));
    //soundPlayer->setVolume(50);
    soundPlayer->play();
}

void SoundManager::PlayBGSE(QString path,bool loop)
{

    if(loop)
    {
        bgse->clear();
        bgsePlayer->setPlaylist(bgm);
        bgse->addMedia(QUrl::fromLocalFile(path));
        bgse->setPlaybackMode(QMediaPlaylist::Loop);
        bgsePlayer->play();
        return;
    }
    if(bgsePlayer->state() == QMediaPlayer::QMediaPlayer::State::PausedState)
    {
        bgsePlayer->play();
        return;
    }
    bgsePlayer->setMedia(QUrl::fromLocalFile(path));
    //bgsePlayer->setVolume(50);
    bgsePlayer->play();
}

void SoundManager::PlayAppSound(QString path)
{
    if(!appPlayer)
        appPlayer = new QMediaPlayer();
    appPlayer->setMedia(QUrl::fromLocalFile(path));
    appPlayer->play();
}

void SoundManager::Pause(SoundManager::SOUND_TYPE t)
{
    switch(t)
    {
        case BGM:
            bgmPlayer->pause();
            break;
        case BGSE:
            bgsePlayer->pause();
            break;
        case SOUND:
            soundPlayer->pause();
            break;
    }
}

void SoundManager::Stop(SoundManager::SOUND_TYPE t)
{
    switch(t)
    {
        case BGM:
            bgmPlayer->stop();
            break;
        case BGSE:
            bgsePlayer->stop();
            break;
        case SOUND:
            soundPlayer->stop();
            break;
    }
}
void SoundManager::SetVolume(float volume,SOUND_TYPE t)
{
    switch(t)
    {
        case BGM:
            bgmPlayer->setVolume(volume);
            GameManager::Instance().soundVolume = volume;
            break;
        case BGSE:
            bgsePlayer->setVolume(volume);
            GameManager::Instance().bgmVolume = volume;
            break;
        case SOUND:
            soundPlayer->setVolume(volume);
            GameManager::Instance().bgseVolume = volume;
            break;
    }
}

float SoundManager::GetTotalTime(SOUND_TYPE t)
{
    switch(t)
    {
        case BGM:
            return bgmPlayer->duration();
            break;
        case BGSE:
            return bgsePlayer->duration();
            break;
        case SOUND:
            return soundPlayer->duration();
            break;
    }
}

float SoundManager::GetPosition(SOUND_TYPE t)
{
    switch(t)
    {
        case BGM:
            return bgmPlayer->position();
            break;
        case BGSE:
            return bgsePlayer->position();
            break;
        case SOUND:
            return soundPlayer->position();
            break;
    }
}

QMediaPlayer *SoundManager::GetPlayer(SoundManager::SOUND_TYPE t)
{
    switch(t)
    {
        case BGM:
            return bgmPlayer;
            break;
        case BGSE:
            return bgsePlayer;
            break;
        case SOUND:
            return soundPlayer;
            break;
    }
}
